<?php


namespace App\Utils\Traits;


use App\Enum\AppEnum;
use App\Utils\Helper\GetterHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;

trait ControllerTrait {
    private $security;

    public function __construct (Security $security) {
        $this->security = $security;
    }

    /**
     * 校验传入对象当前用户是否可以访问
     * @param $object
     * @return null
     */
    public function verify ($object) {
        if (!$object) {
            throw $this->createNotFoundException(sprintf('unable to find the object'));
        }

        if ($this->security->getUser()->isSuperAdmin()) {
            return null;
        }

        if ($object->getAdminUserId() != $this->security->getUser()->getId()) {
            throw new AccessDeniedException('无权操作该对象');
        }

        return null;
    }

    /**
     * 日期过滤
     * @param string $alias
     * @param FieldDescription $filterField
     * @param $value
     * @return string|null
     */
    private function datetimeBuildWhere (string $alias, FieldDescription $filterField, $value) {
        $result = null;
        if ($value) {
            $dataArr = explode(' - ', $value);
            if (count($dataArr) == 2) {
                $start = $dataArr[0].' 00:00:00';
                $end = $dataArr[1].' 23:59:59';
                $result = $alias.".".$filterField->getFieldName()." between '".$start."' and '".$end."'";
            }
        }
        return $result;
    }

    /**
     * 字符串过滤
     * @param string $alias
     * @param FieldDescription $filterField
     * @param $value
     * @return string|null
     */
    private function stringBuildWhere (string $alias, FieldDescription $filterField, $value) {
        $result = null;
        if ($value != '') {
            $result = $alias.".".$filterField->getFieldName()." like '%".$value."%'";
        }
        return $result;
    }

    /**
     * 布尔值过滤
     * @param string $alias
     * @param FieldDescription $filterField
     * @param $value
     * @return string|null
     */
    private function booleanBuildWhere (string $alias, FieldDescription $filterField, $value) {
        $result = null;
        if ($value) {
            $result = $alias.".".$filterField->getFieldName()." = ".$value;
        }
        return $result;
    }

    /**
     * 整型过滤
     * @param string $alias
     * @param FieldDescription $filterField
     * @param $value
     * @return string|null
     */
    private function integerBuildWhere (string $alias, FieldDescription $filterField, $value) {
        $result = null;
        if ($value != '') {
            $result = $alias.".".$filterField->getFieldName()." = ".$value;
        }
        return $result;
    }

    /**
     * app mode 校验
     * @param Request $request
     * @param $object
     * @param AdminInterface $admin
     * @param string $action
     * @return mixed
     */
    public function verifyAppMode (Request $request, $object, AdminInterface $admin, $action = 'edit') {
        $app_mode = GetterHelper::getContainer()->getParameter('app.mode');

        if ($action == 'create') {
            if ($request->getMethod() == 'POST') {
                if ($app_mode == AppEnum::MODE_DEMO) {
                    $this->addFlash('sonata_flash_error', 'Demo模式下禁止添加数据');
                    return $this->redirectToList();
                }
            }
        }
        if ($action == 'edit') {
            if ($request->getMethod() == 'POST') {
                if ($app_mode == AppEnum::MODE_DEMO) {
                    $this->addFlash('sonata_flash_error', 'Demo模式下禁止编辑数据');
                    return $this->redirectTo($object);
                }
            }
        }
        if ($action == 'delete') {
            if (Request::METHOD_DELETE === $this->getRestMethod()) {
                if ($app_mode == AppEnum::MODE_DEMO) {
                    $this->addFlash('sonata_flash_error', 'Demo模式下禁止删除数据');
                    return $this->redirectTo($object);
                }
            }
        }
    }

    /**
     * 判断当前是否为demo模式
     * @return bool
     */
    public function isAppModeDemo () {
        $app_mode = GetterHelper::getContainer()->getParameter('app.mode');
        return $app_mode == AppEnum::MODE_DEMO;
    }
}